import unittest
from TicTacToeGame import TicTacToe
from random import choice

class TestTicTacToe(unittest.TestCase):
	def setUp(self):
		self.game = TicTacToe()
		self.listOfMoves = range(0, 9)
	
	def testplaceMarkerIllegalInput(self):
		self.assertRaises(ValueError, self.game.placeMarker, 1, -1)
		self.assertRaises(ValueError, self.game.placeMarker, '', 'a')
		self.assertRaises(ValueError, self.game.placeMarker, 'a', 13)
		self.assertRaises(ValueError, self.game.placeMarker, 2, 13)
			
	def testMoveLegalInput(self):
		move = choice(self.listOfMoves)
		self.listOfMoves.remove(move)
		result = self.game.placeMarker(1, move)
		self.assertTrue(not result)
		self.assertTrue(self.game.board[move] == 1)
			
	def testplaceMarkerOccupiedSpace(self):
		move = choice(self.listOfMoves)
		self.game.placeMarker(1, move)
		self.assertRaises(ValueError, self.game.placeMarker, 2, move)
	
	def testUpdateWinList(self):
		move = choice(self.listOfMoves)
		self.game.updateWinList(move, self.game.player1WinList)
		
		for winType in self.game.player1WinList:
			self.assertTrue(move not in winType)
			self.assertTrue(winType)
		
		self.assertTrue(self.game.player1WinList)
			
	def testRemoveFromWinList(self):
		move = choice(self.listOfMoves)
		self.game.removeFromWinList(move, self.game.player1WinList)

		for winType in self.game.player1WinList:
			self.assertTrue(move not in winType)
		
		self.assertTrue(self.game.player1WinList)
			
	def testCheckResultsWin(self):
		self.assertFalse(self.game.placeMarker(1, 1))
		self.assertFalse(self.game.placeMarker(2, 3))
		self.assertFalse(self.game.placeMarker(1, 4))
		self.assertFalse(self.game.placeMarker(2, 0))
		self.assertTrue(self.game.placeMarker(1, 7), 1)
		
		self.assertTrue([] in self.game.player1WinList)
		self.assertTrue(self.game.player2WinList)
		
	def testCheckResultsDraw(self):
		self.assertEqual(self.game.placeMarker(1, 0), None)
		self.assertEqual(self.game.placeMarker(2, 4), None)
		self.assertEqual(self.game.placeMarker(1, 2), None)
		self.assertEqual(self.game.placeMarker(2, 1), None)
		self.assertEqual(self.game.placeMarker(1, 7), None)
		self.assertEqual(self.game.placeMarker(2, 8), None)
		self.assertEqual(self.game.placeMarker(1, 3), None)
		self.assertEqual(self.game.placeMarker(2, 6), 0)
		
		self.assertFalse(self.game.player1WinList)
		self.assertFalse(self.game.player2WinList)
		
def suite():
	suite = unittest.TestLoader().loadTestsFromTestCase(TestTicTacToe)
	return suite